# --------------------------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------------------------
set-alias dd Debug-Display -Option AllScope -Description "PSCX function alias"

function Debug-Display ( $TestVar="", $Message)
{
  function write-confirm
  {
    Log-Message "?????????????????????????????????????????????????????????????????????????????????????????????" -ForegroundColor Yellow
    Log-Message "Debug $Message" -backgroundColor White -foregroundcolor Black
    Log-Message
    if ($TestVar.length -gt 0)
    {
      Display-Variable $TestVar
    }
  }

  write-confirm
  while ($TRUE)
  {
    $answer = Read-Host @"
[C] Continue  [X] Exit  [S] Suspend (default is "C")
"@
    switch ($answer)
    { "C"   { return }
      ""    { return }
      "X"   { exit }
      "S"   { $Host.EnterNestedPrompt(); write-confirm }
    }
  }
}

# --------------------------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------------------------
set-alias dv Display-Variable -Option AllScope -Description "PSCX function alias"

function Display-Variable ( $obj, $display="", $indent="")
{

  if ($obj -eq $null)
  {
    $type = $null
  }
  else
  {
    $type = $obj.gettype().name
  }
  
  $quote = '"'
  #Log-Message "Var $type"
  switch ($type)
  {
    "PSNoteProperty"
    {
        Display-Variable $obj.value $newDisplay "$indent  "
    }
    "PSCustomObject"
    {
      Log-Message "$indent$display" 
      foreach ($member in $obj.psobject.members)
      {
        #Log-Message "got prop $($member.name)"
        if ($member.gettype().name -eq "PSNoteProperty" -and (!$member.name.StartsWith("__")))
        {
          $newDisplay = ".$($member.name)"
          Display-Variable $member.Value $newDisplay "$indent  "
        }
      }
    }
    { $_ -eq "Object[]" -or $_ -eq "ArrayList" } 
    { 
      Log-Message "$indent$display = [array]" 
      $cnt = 0
      foreach ($item in $obj)
      {
        $newDisplay = '[' + $cnt + ']'
        Display-Variable $obj[$cnt] $newDisplay "$indent  "
        $cnt = $cnt + 1
      }
    }
    "Hashtable" 
    { 
      Log-Message "$indent$display = [hashtable]" 
      $keys = $obj.keys
      foreach ( $key in $keys)
      {
        $newDisplay = '["' + $key + '"]'
        Display-Variable $obj[$key] $newDisplay "$indent  "
      }
    }
    "XmlDocument" 
    {
      Log-Message "$indent$display = [xmldocument]"
      $props = $obj | get-member | ?{$_.MemberType -eq "Property" -and $_.Name -ne "xml"}
      foreach ($prop in $props)
      {
        $newDisplay = '.' + $prop.name
        Display-Variable $obj.$($prop.name) $newDisplay "$indent  "
      }
    }    
    "XmlElement"
    {
      Log-Message "$indent$display = [xmlnode]" 
      $props = $obj | get-member | ?{$_.MemberType -eq "Property" -and $_.Name -ne "xsd" -and $_.Name -ne "xsi"}
      foreach ($prop in $props)
      {
        $newDisplay = '.' + $prop.name
        Display-Variable $obj.$($prop.name) $newDisplay "$indent  "
      }
    }    
    "String"
    {
      $msg = "$indent$display = [string] " + $quote
      Log-Message $msg -noNewLine
      Log-Message "$obj" -backgroundColor Black -foregroundcolor Yellow -noNewLine
      Log-Message $quote
    }
    default
    {
      Log-Message "$indent$display = [$type]" -noNewLine
      Log-Message "$obj" -backgroundColor Black -foregroundcolor Yellow
    } 
  }
}